package com.lifep.collect.platform.module;

import com.lifep.collect.platform.module.classloader.ClassLoaderCaches;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * @author: zw
 * @create: 2024-09-28 22:18
 * @description:
 **/
@Slf4j
public class DmcDynamicModule extends DmcSpringModule {

    private final ConfigurableApplicationContext applicationContext;

    public DmcDynamicModule(String dataProvider, ConfigurableApplicationContext applicationContext) {
        super(dataProvider, applicationContext);
        this.applicationContext = applicationContext;
    }

    @Override
    public void destroy() {
        super.destroy();
        ClassLoader classLoader = applicationContext.getClassLoader();
        closeQuietly(applicationContext);
        ClassLoaderCaches.clear(classLoader);
    }

    private static void closeQuietly(ConfigurableApplicationContext applicationContext) {
        try {
            applicationContext.close();
        } catch (Exception e) {
            log.error("unknown exception while close module application context, e = {}", e);
        }
    }
}
